<?php

require_once('../../config.php');
require_once($CFG->dirroot . '/blocks/myguestbook/lib.php');

require_once($CFG->dirroot . '/lib/outputcomponents.php');
require_once($CFG->libdir . '/outputcomponents.php');
require_once($CFG->dirroot . '/blocks/myguestbook/block_myguestbook_editable_form.php');
require_once($CFG->dirroot . '/blocks/myguestbook/edit.php');

global $OUTPUT, $PAGE, $CFG, $COURSE;

require_login();

$PAGE->set_context(context_course::instance($COURSE->id));
$PAGE->set_url('/blocks/myguestbook/edit.php');
$PAGE->set_pagelayout('standard');
$PAGE->set_heading(get_string('pluginname', 'block_myguestbook'));
$PAGE->set_title(get_string('pluginname', 'block_myguestbook'));

$PAGE->requires->css(new moodle_url('/blocks/myguestbook/style/myguestbook.css'));

$cform = new block_myguestbook_editable_form();
$eform = new block_myguestbook_editable_form();

// навигация
$PAGE->navbar->ignore_active();
$PAGE->navbar->add(get_string(
                'pluginname', 'block_myguestbook'), new moodle_url('/blocks/myguestbook/view.php'));
$previewnode = $PAGE->navigation->add(get_string(
                'pluginname', 'block_myguestbook'), new moodle_url('/blocks/myguestbook/view.php'),
        navigation_node::TYPE_CONTAINER
);

// Забираем массив всех сообщений из базы данных
$data = get_messages();

$messageid = optional_param('messageid', null, PARAM_INT);
$toform = array();
$toedit = array();
$main_url = new moodle_url('/blocks/myguestbook/view.php');
$this_url = new moodle_url('/blocks/myguestbook/edit.php');


if ($eform->is_cancelled() || $cform->is_cancelled()) {
    redirect($main_url);
} else if (optional_param('edparam', null, PARAM_TEXT) === 'Edit') {

    // КОСТЫЛЬ

    foreach ($data as $d) {
        if ($d->id == $messageid) {
            $toedit['id']      = $messageid;   
            $toedit['type']    = 'Edit';
            $toedit['name']    = $d->name;
            $toedit['time']    = $d->time;
            $toedit['message'] = $d->message;
        }
    }

    // КОСТЫЛЬ

    $eform->set_data($toedit);
    view($eform);

} else if (optional_param('edparam', null, PARAM_TEXT) === 'Comment') {
    // КОСТЫЛЬ

    foreach ($data as $d) {
        if ($d->id == $messageid) {
            $touse['id']       = $messageid;   
            $touse['type']     = 'Comment';
            $toform['id']      = $messageid;   
            $toform['name']    = $d->name;
            $toform['time']    = $d->time;
            $toform['message'] = $d->message;
        }
    }

    // КОСТЫЛЬ
    $cform->set_data($touse);
    view($cform, $toform);
    
} else if (($fromform = $eform->get_data()) && ($fromform->type == 'Edit')) {
    update_message($fromform->id, $fromform->message);
    redirect($main_url);

} else if (($fromform = $cform->get_data()) && ($fromform->type == 'Comment')) {
    set_comment($fromform->name, $fromform->message, time(), $fromform->id);
    redirect($main_url);
}    

// отображение хедера, сообщения с комментариями (если надо), формы и футера

function view($mform, $toform = null) {
    global $OUTPUT;

    echo $OUTPUT->header();

    if (!empty($toform)) {
        show($toform);
    }

    echo $OUTPUT->container_start('form_wrap');
    $mform->display();
    echo $OUTPUT->container_end();

    echo $OUTPUT->footer();
}

// Отображение сообщения и комментариев к нему
function show($toform) {
    global $OUTPUT;

    $data_c = get_comments($toform['id']);
    
    echo $OUTPUT->container(
            $OUTPUT->notification(
                    $toform['name'], 'name'
            ) .
            $OUTPUT->notification(
                    date("H:i:s", $toform['time']), 'time'
            ) .
            $OUTPUT->notification(
                    $toform['message'], 'message'
            ), 
            "wrap_post"
    );
    
    foreach ($data_c as $num => $comment) {
            echo $OUTPUT->container_start('wrap_comment');
                echo $OUTPUT->notification($comment->name, 'name');
                echo $OUTPUT->notification(date("H:i:s", $comment->time), 'time');
                echo $OUTPUT->container($comment->comment, 'message');
            echo $OUTPUT->container_end();
    }
    
}
